<?PHP
if (!session_start()) { session_start(); };
/*if (!isset($_SESSION['lgnapproved'])) {
	die(header('Location: login.php'));
}*/

include("functions.php");
include("cnnxn.php");
connect();

if (isset($_POST['submit'])) {
	
	$arrayFlag=0;
	$submitArray=array();
	$submitFlag=1;

	$fieldnames="";
	$fieldvalues="";
	
	$SQLged='SELECT * FROM Events WHERE EventID='.$_POST['EventID'].';';
	$sendged=mysqli_query($db,$SQLged) or die(mysqli_error($db));
	$RSged=mysqli_fetch_object($sendged);
	$TeamSize=$RSged->TeamSize;
	
	$SQLcte='SELECT * FROM Teams ORDER BY TeamID DESC LIMIT 1;';
	$sendcte=mysqli_query($db,$SQLcte) or die(mysqli_error($db));
	$RScte=mysqli_fetch_object($sendcte);
	$newTeamID=$RScte->TeamID+1;
	
	foreach ($_POST as $key=>$value) {
		if ($key!='EventID' && $key!='submit' && $key!='PHPSESSID') {
			
			if ($submitFlag<=$TeamSize) {
				$SQLaddt='INSERT INTO Teams (TeamID,PlayerID) VALUES ('.$newTeamID.','.$value.');';
				//print $SQLaddt.'<br />';
				$sendit_addt=mysqli_query($db,$SQLaddt) or die(mysqli_error($db));
				$submitFlag++;
			} else {
				$SQLaddr='INSERT INTO Results (EventID,TeamID,Result) VALUES ('.$_POST['EventID'].','.$newTeamID.','.$value.');';
				//print $SQLaddr.'<br /><br />';
				$sendr=mysqli_query($db,$SQLaddr) or die(mysqli_error($db));
				$submitFlag=1;
				$newTeamID++;
			}
			
		}
	}
	die(header("Location: Events_viewall.php?tid=".$_GET['tid']));
}
?>